﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using System.Collections.Generic;


public partial class APP_frmA_CompHistoryInput : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = @"本社履歴情報登録";
        ((APP_MasterPage_appCom)this.Master).changeTitle("本社履歴情報登録");
        ((APP_MasterPage_appCom)this.Master).HideLiArea("frmA_CompHistoryInput");
        if (!IsPostBack)
        {
            getYear();
        }
        if (Session["役"] == null || Session["役"] == "" || Session["役"] == "1")
        {
            Response.Redirect("~/frmD_Login.aspx");
        }
        else
        {
            if (Session["Update_frmA_History_Fom_HistoryInfo"] != null)
            {
                if (Session["GetYar_Fom_HistoryInfo"] != null)
                {
                    this.get_CompHistoryInfo();
                    ddl_年度.Enabled = false;
                }
            }
            ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "regis", "Disable();CheckReady();", true);
            ddl_年度.Focus();
            CheckSession();
            validateddl_年度.ErrorMessage = GetError("ER00007");
            validatetxt_売上.ErrorMessage = GetError("ER00007");
            validatetxt_営業損益.ErrorMessage = GetError("ER00001");
            validatetxt_経常損益.ErrorMessage = GetError("ER00001");
            validatetxt_当期純損益.ErrorMessage = GetError("ER00001");
            validatetxt_常勤事務職_男性.ErrorMessage = GetError("ER00001");
            validatetxt_常勤事務職_女性.ErrorMessage = GetError("ER00001");
            validatetxt_常勤現場職_男性.ErrorMessage = GetError("ER00001");
            validatetxt_常勤現場職_女性.ErrorMessage = GetError("ER00001");
            //validatetxt_非常勤事務職_男性.ErrorMessage = "非常勤事務職_男性" + GetError("ER00001");
            //validatetxt_非常勤事務職_女性.ErrorMessage = "非常勤事務職_女性" + GetError("ER00001");
            validatetxt_年間労働日数.ErrorMessage = GetError("ER00001");
            //validatetxt_勤務開始時間.ErrorMessage = "勤務開始時間" + GetError("ER00001");
            CustomValidator4.ErrorMessage = GetError("ER00001");
            //validatetxt_勤務終了時間.ErrorMessage = "勤務終了時間" + GetError("ER00001");
            CustomValidator20.ErrorMessage = GetError("ER00001");
            CustomValidator1.ErrorMessage =  GetError("ER00006");
            CustomValidator2.ErrorMessage =  GetError("ER00006");
            //regular1.ErrorMessage = "勤務開始時間" + GetError("ER00001");
            CustomValidator6.ErrorMessage =  GetError("ER00001");
            //regular2.ErrorMessage = "勤務終了時間" + GetError("ER00001");
            CustomValidator5.ErrorMessage =  GetError("ER00002");
            CustomValidator3.ErrorMessage = GetError("ER00022");
            RequiredFieldValidator2.ErrorMessage =  GetError("ER00001");
            //RequiredFieldValidator1.ErrorMessage =  GetError("ER00001");
            //Regular1.ErrorMessage = GetError("ER00002");
            //CustomValidator7.ErrorMessage = GetError("ER00009");
            CustomValidator17.ErrorMessage = GetError("ER00002");
            CustomValidator18.ErrorMessage = GetError("ER00002");
            CustomValidator19.ErrorMessage = GetError("ER00002");
            CustomValidator23.ErrorMessage = GetError("ER00002");
            CustomValidator21.ErrorMessage = GetError("ER00006");
            CustomValidator25.ErrorMessage = GetError("ER00006");
            CustomValidator28.ErrorMessage = GetError("ER00002");
            CustomValidator29.ErrorMessage = GetError("ER00006");
        }
    }
    protected void btn_確認_Click(object sender, EventArgs e)
    {
        try
        {

            //if (CustomValidator7.IsValid == true)
            //{
                List<List<String>> list = new List<List<String>>();
                String[] varList = Request.Form.AllKeys;
                for (int i = 1; i < varList.Length; i++)
                {
                    string controlName = varList[i];
                    string value = Request.Params.Get(controlName).Replace("<", "&lt;");
                    value = value.Replace(">", "&gt;");
                    string serverControlName = controlName.Replace("ctl00$ContentPlaceHolder1$", "");
                    List<String> val = new List<String>();
                    val.Add(serverControlName);
                    val.Add(value);
                    list.Add(val);
                }

                Session["listValue_CompHistoryInput"] = list;


                Response.Redirect("frmA_CompHistoryCheck.aspx");
            }
        //}
        catch { }
    }
    public void getYear()// kiem tra nam.
    {
        string a = DateTime.Now.Year.ToString();
        for (int i = Int32.Parse(a)-9; i <= Int32.Parse(a); i++)
        {
            ListItem item = new ListItem(i.ToString(),i.ToString());
            ddl_年度.Items.Add(item);
        }

    }

    public String GetError(string errorid) // truyen ma id lay loi
    {
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        return dt.Rows[0][2].ToString();
    }
    public void CheckSession()//  Session dung de show thong tin khi nguoi dung click btn_確認_Click o trang CompHistoryCheck de quay ve trang CompHistoryInput.aspx.
    {
        try
        {
            if (Session["listValue_CompHistoryInput"] != null)
            {
                List<List<String>> list = new List<List<String>>();
                list = (System.Collections.Generic.List<List<String>>)Session["listValue_CompHistoryInput"];
                

                foreach (List<String> str in list)
                {
                    if (str[0] == "ddl_年度")
                    {
                        ddl_年度.Text = str[1];
                    }
                    if (str[0] == "ddl_決算月")
                    {
                        ddl_決算月.Text = str[1] ;
                    }
                    if (str[0] == "txt_売上")
                    {
                        txt_売上.Text = str[1];
                    }
                    if (str[0] == "txt_営業損益")
                    {
                        txt_営業損益.Text = str[1];
                    }

                    if (str[0] == "txt_経常損益")
                    {
                        txt_経常損益.Text = str[1];
                    }
                    if (str[0] == "txt_当期純損益")
                    {
                        txt_当期純損益.Text = str[1];
                    }

                    if (str[0] == "txt_常勤事務職_男性")
                    {
                        txt_常勤事務職_男性.Text = str[1];
                    }

                    if (str[0] == "txt_常勤事務職_女性")
                    {
                        txt_常勤事務職_女性.Text = str[1];
                    }

                    if (str[0] == "txt_常勤現場職_男性")
                    {
                        txt_常勤現場職_男性.Text = str[1];
                    }

                    if (str[0] == "txt_常勤現場職_女性")
                    {
                        txt_常勤現場職_女性.Text = str[1];
                    }

                    if (str[0] == "txt_非常勤事務職_男性")
                    {
                        txt_非常勤事務職_男性.Text = str[1];
                    }

                    if (str[0] == "txt_非常勤事務職_女性")
                    {
                        txt_非常勤事務職_女性.Text = str[1];
                    }

                    if (str[0] == "txt_大卒初任給")
                    {
                        txt_大卒初任給.Text = str[1];
                    }

                    if (str[0] == "txt_高卒初任給")
                    {
                        txt_高卒初任給.Text = str[1];
                    }

                    if (str[0] == "txt_中卒初任給")
                    {
                        txt_中卒初任給.Text = str[1];
                    }
                    if (str[0] == "txt_年間労働日数")
                    {
                        txt_年間労働日数.Text = str[1];
                    }

                    if (str[0] == "txt_勤務開始時間")
                    {
                        txt_勤務開始時間.Text = str[1];
                    }

                    if (str[0] == "ddl_昼休み開始時間")
                    {
                        ddl_昼休み開始時間.Text = str[1];
                    }

                    if (str[0] == "ddl_昼休み終了時間")
                    {
                        ddl_昼休み終了時間.Text = str[1];
                    }

                    if (str[0] == "txt_勤務終了時間")
                    {
                        txt_勤務終了時間.Text = str[1];
                    }

                    if (str[0] == "txt_休憩合計時間")
                    {
                        txt_休憩合計時間.Text = str[1];
                    }

                    if (str[0] == "txt_所定労働時間")
                    {
                        txt_所定労働時間.Text = str[1];
                    }

                    if (str[0] == "txt_所定労働時間_分")
                    {
                        txt_所定労働時間_分.Text = str[1];
                    }

                    if (str[0] == "ddl_週平均労働日数")
                    {
                        ddl_週平均労働日数.Text = str[1];
                    }


                }
            }
        }
        catch { }
    }
    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        if (Session["Update_frmA_History_From_ComBasicCheck"] == null && Session["GetYar_Fom_HistoryInfo"] == null)
        {
            Session.Remove("listValue_CompHistoryInput");
            Response.Redirect("frmA_CompHistoryInput.aspx");
        }
        else if (Session["listValue_CompHistoryInput"] != null)
        {
            CheckSession();
        }
        else
        {
            本社履歴Manager frm = new 本社履歴Manager();
            DataTable dt = new DataTable();
            int 年度 = Int32.Parse(ddl_年度.SelectedValue.ToString());
            int 企業ID = Int32.Parse(Session["企業コード_2"].ToString());
            dt = frm.GetYear本社履歴(年度, 企業ID);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    ddl_年度.SelectedValue = row["年度"].ToString();
                    ddl_決算月.SelectedValue = row["決算月"].ToString();
                    txt_売上.Text = row["売上"].ToString();
                    txt_営業損益.Text = row["営業損益"].ToString();
                    txt_経常損益.Text = row["経営損益"].ToString();
                    txt_当期純損益.Text = row["当期損益"].ToString();
                    txt_常勤事務職_男性.Text = row["常勤事務男"].ToString();
                    txt_常勤事務職_女性.Text = row["常勤事務女"].ToString();
                    txt_常勤現場職_男性.Text = row["常勤現場男"].ToString();
                    txt_常勤現場職_女性.Text = row["常勤現場女"].ToString();
                    txt_非常勤事務職_男性.Text = row["非常勤事務男"].ToString();
                    txt_非常勤事務職_女性.Text = row["非常勤現場女"].ToString();
                    txt_大卒初任給.Text = row["大卒初任給"].ToString();
                    txt_高卒初任給.Text = row["高卒初任給"].ToString();
                    txt_中卒初任給.Text = row["中卒初任給"].ToString();
                    txt_年間労働日数.Text = row["年間労働日数"].ToString();
                    txt_勤務開始時間.Text = row["勤務開始時間"].ToString();
                    ddl_昼休み開始時間.Text = row["昼休み開始時間"].ToString();
                    ddl_昼休み終了時間.Text = row["昼休み終了時間"].ToString();
                    txt_勤務終了時間.Text = row["勤務終了時間"].ToString();
                    txt_休憩合計時間.Text = row["休憩時間"].ToString();
                    if (Int32.Parse(row["所定労働時間"].ToString()) < 10)
                    {
                        txt_所定労働時間.Text = "0" + row["所定労働時間"].ToString();
                    }
                    else
                    {
                        txt_所定労働時間.Text =  row["所定労働時間"].ToString();
                    }
                    if (Int32.Parse(row["所定労働時間_分"].ToString()) < 10)
                    {
                        txt_所定労働時間_分.Text = "0" + row["所定労働時間_分"].ToString();
                    }
                    else
                    {
                        txt_所定労働時間_分.Text =  row["所定労働時間_分"].ToString();
                    }
                    ddl_週平均労働日数.Text = row["平均週間労働日数"].ToString();


                }
            }
        }

    }


    //protected void CustomValidate年度(object source, ServerValidateEventArgs args)
    //{
    //    try
    //    {
    //        if (Session["IdEmp"] != null)
    //        {
    //            本社履歴Manager frm = new 本社履歴Manager();
    //            DataTable dt = new DataTable();
    //            int 年度 = Int32.Parse(ddl_年度.SelectedValue.ToString());
    //            int 企業ID = 189; // ID lay tu sesssion khi dang ky tu trang frmJ_StaffBasicInfoInput;
    //            dt = frm.GetYear本社履歴(年度, 企業ID);
    //            if (dt.Rows.Count > 0)
    //            {
    //                args.IsValid = false;
    //                return;
    //            }
    //            else
    //            {
    //                args.IsValid = true;
    //            }
    //        }
    //    }
    //    catch { }
    //}

    public void get_CompHistoryInfo()
    {
        本社履歴Manager frm = new 本社履歴Manager();
        DataTable dt = new DataTable();
        int 年度 = Int32.Parse(Session["GetYar_Fom_HistoryInfo"].ToString());
        int 企業ID = Int32.Parse(Session["企業コード_2"].ToString()); // ID lay tu sesssion khi dang ky tu trang frmJ_StaffBasicInfoInput;
        dt = frm.GetYear本社履歴(年度, 企業ID);
       
            foreach (DataRow row in dt.Rows)
            {
                ddl_年度.SelectedValue = row["年度"].ToString();
                ddl_決算月.SelectedValue = row["決算月"].ToString();
                txt_売上.Text = row["売上"].ToString();
                txt_営業損益.Text = row["営業損益"].ToString();
                txt_経常損益.Text = row["経営損益"].ToString();
                txt_当期純損益.Text = row["当期損益"].ToString();
                txt_常勤事務職_男性.Text = row["常勤事務男"].ToString();
                txt_常勤事務職_女性.Text = row["常勤事務女"].ToString();
                txt_常勤現場職_男性.Text = row["常勤現場男"].ToString();
                txt_常勤現場職_女性.Text = row["常勤現場女"].ToString();
                txt_非常勤事務職_男性.Text = row["非常勤事務男"].ToString();
                txt_非常勤事務職_女性.Text = row["非常勤現場女"].ToString();
                txt_大卒初任給.Text = row["大卒初任給"].ToString();
                txt_高卒初任給.Text = row["高卒初任給"].ToString();
                txt_中卒初任給.Text = row["中卒初任給"].ToString();
                txt_年間労働日数.Text = row["年間労働日数"].ToString();
                txt_勤務開始時間.Text = row["勤務開始時間"].ToString();
                ddl_昼休み開始時間.Text = row["昼休み開始時間"].ToString();
                ddl_昼休み終了時間.Text = row["昼休み終了時間"].ToString();
                txt_勤務終了時間.Text = row["勤務終了時間"].ToString();
                txt_休憩合計時間.Text = row["休憩時間"].ToString();
                if (Int32.Parse(row["所定労働時間"].ToString()) < 10)
                {
                    txt_所定労働時間.Text = "0" + row["所定労働時間"].ToString();
                }
                else
                {
                    txt_所定労働時間.Text = row["所定労働時間"].ToString();
                }
                if (Int32.Parse(row["所定労働時間_分"].ToString()) < 10)
                {
                    txt_所定労働時間_分.Text = "0" + row["所定労働時間_分"].ToString();
                }
                else
                {
                    txt_所定労働時間_分.Text = row["所定労働時間_分"].ToString();
                }
                ddl_週平均労働日数.Text = row["平均週間労働日数"].ToString();


            
        }
    }


    protected void ddl_年度_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Session["listValue_CompHistoryInput"] != null)
        {
            CheckSession();
        }
        else
        {
            本社履歴Manager frm = new 本社履歴Manager();
            DataTable dt = new DataTable();
            int 年度 = Int32.Parse(ddl_年度.SelectedValue.ToString());
            int 企業ID = Int32.Parse(Session["企業コード_2"].ToString());
            dt = frm.GetYear本社履歴(年度, 企業ID);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    ddl_年度.SelectedValue = row["年度"].ToString();
                    ddl_決算月.SelectedValue = row["決算月"].ToString();
                    txt_売上.Text = row["売上"].ToString();
                    txt_営業損益.Text = row["営業損益"].ToString();
                    txt_経常損益.Text = row["経営損益"].ToString();
                    txt_当期純損益.Text = row["当期損益"].ToString();
                    txt_常勤事務職_男性.Text = row["常勤事務男"].ToString();
                    txt_常勤事務職_女性.Text = row["常勤事務女"].ToString();
                    txt_常勤現場職_男性.Text = row["常勤現場男"].ToString();
                    txt_常勤現場職_女性.Text = row["常勤現場女"].ToString();
                    txt_非常勤事務職_男性.Text = row["非常勤事務男"].ToString();
                    txt_非常勤事務職_女性.Text = row["非常勤現場女"].ToString();
                    txt_大卒初任給.Text = row["大卒初任給"].ToString();
                    txt_高卒初任給.Text = row["高卒初任給"].ToString();
                    txt_中卒初任給.Text = row["中卒初任給"].ToString();
                    txt_年間労働日数.Text = row["年間労働日数"].ToString();
                    txt_勤務開始時間.Text = row["勤務開始時間"].ToString();
                    ddl_昼休み開始時間.Text = row["昼休み開始時間"].ToString();
                    ddl_昼休み終了時間.Text = row["昼休み終了時間"].ToString();
                    txt_勤務終了時間.Text = row["勤務終了時間"].ToString();
                    txt_休憩合計時間.Text = row["休憩時間"].ToString();
                    if (Int32.Parse(row["所定労働時間"].ToString()) < 10)
                    {
                        txt_所定労働時間.Text = "0" + row["所定労働時間"].ToString();
                    }
                    else
                    {
                        txt_所定労働時間.Text = row["所定労働時間"].ToString();
                    }
                    if (Int32.Parse(row["所定労働時間_分"].ToString()) < 10)
                    {
                        txt_所定労働時間_分.Text = "0" + row["所定労働時間_分"].ToString();
                    }
                    else
                    {
                        txt_所定労働時間_分.Text = row["所定労働時間_分"].ToString();
                    }
                    ddl_週平均労働日数.Text = row["平均週間労働日数"].ToString();


                }
                Session["Update_frmA_History_From_ComBasicCheck"] = "Update";
            }
            else
            {

                //ddl_年度.SelectedValue = "-1";
                ddl_決算月.SelectedValue = "-1";
                txt_売上.Text = "";
                txt_営業損益.Text = "";
                txt_経常損益.Text = "";
                txt_当期純損益.Text = "";
                txt_常勤事務職_男性.Text = "";
                txt_常勤事務職_女性.Text = "";
                txt_常勤現場職_男性.Text = "";
                txt_常勤現場職_女性.Text = "";
                txt_非常勤事務職_男性.Text = "";
                txt_非常勤事務職_女性.Text = "";
                txt_大卒初任給.Text = "";
                txt_高卒初任給.Text = "";
                txt_中卒初任給.Text = "";
                txt_年間労働日数.Text = "";
                txt_勤務開始時間.Text = "";
                ddl_昼休み開始時間.Text = "";
                ddl_昼休み終了時間.Text = "";
                txt_勤務終了時間.Text = "";
                txt_休憩合計時間.Text = "";
                txt_所定労働時間.Text = "";
                txt_所定労働時間_分.Text = "";
                ddl_週平均労働日数.SelectedValue = "";
                Session.Remove("Update_frmA_History_From_ComBasicCheck");
            }
        }
    }
    protected void btn_次_Click(object sender, EventArgs e)
    {
        if (Session["Update_frmA_History_Fom_HistoryInfo"] != null)
        {
            Session.Remove("listValue_CompHistoryInput");
            Session.Remove("Update_frmA_History_From_ComBasicCheck");
            Session.Remove("GetYar_Fom_HistoryInfo");
            Session.Remove("Update_frmA_History_Fom_HistoryInfo");

            Response.Redirect("frmA_CompHistoryInfo.aspx");
        }
        else
        {


            Session.Remove("listValue_CompHistoryInput");
            Session.Remove("Update_frmA_History_From_ComBasicCheck");
            Session.Remove("GetYar_Fom_HistoryInfo");
            Session.Remove("Update_frmA_History_Fom_HistoryInfo");

            Response.Redirect("frmA_CompBasicInfoCheck.aspx");
        }
    }
}
